/* ** por compatibilidad se omiten tildes **
================================================================================
 TRABAJO PRACTICO 3 - System Programming - ORGANIZACION DE COMPUTADOR II - FCEN
================================================================================
  definicion de funciones del scheduler
*/

#include "sched.h"
int frozen = 0;

unsigned short sched_proximo_indice() {
	unsigned short res;
	if (frozen == 1){
		res = 0;
	}
	else{
		if (j_actual == 'N'){
			res = 0;
		}
		else{
			if (j_actual == 'A'){
				if (vivosB() == 0){
					res = proxA()+15;
				}
				else{
					res = proxB()+23;
					j_actual = 'B';
				}
			}
			else{
				if (vivosA() ==0){
					res = proxB()+23;
				}
				else{
					res = proxA()+15;
					j_actual = 'A';
				}
			}
		}
	}	
	return (res<<3);
}

unsigned short proxA(){
	indice_A = (indice_A+1)%8;
	while(indice_A < 8 && zombisAvivos[indice_A]==0)
		indice_A++;
	if (indice_A ==8){
		indice_A = 0;
		while(indice_A < 8 && zombisAvivos[indice_A]==0)
			indice_A++;
	}
	return indice_A;
}

unsigned short proxB(){
	indice_B = (indice_B+1)%8;
	while(indice_B < 8 && zombisBvivos[indice_B]==0)
		indice_B++;
	if (indice_B ==8){
		indice_B = 0;
		while(indice_B < 8 && zombisBvivos[indice_B]==0)
			indice_B++;
	}
	return indice_B;
}

int vivosA(){
	int res = 0;
	int i;
	for (i = 0; i < 8; i++){
		res+= zombisAvivos[i];
	}
	return res;
}

int vivosB(){
	int res = 0;
	int i;
	for (i = 0; i < 8; i++){
		res+= zombisBvivos[i];
	}
	return res;
}